/*
 * DataBarangForm.java
 *
 * Created on November 10, 2011, 8:03 PM
 */
package Transaksi;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author  Elisa
 */
public class DataBarangForm extends javax.swing.JPanel {

    /** Creates new form DataBarangForm */
    public DataBarangForm() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dataBarangDesktopPane = new javax.swing.JDesktopPane();
        jPanel1 = new javax.swing.JPanel();
        kodeBarangLabel = new javax.swing.JLabel();
        namaBarangLabel = new javax.swing.JLabel();
        hargaBeliLabel = new javax.swing.JLabel();
        hargaJualLabel = new javax.swing.JLabel();
        jumlahLabel = new javax.swing.JLabel();
        kodeBarangTextField = new javax.swing.JTextField();
        namaBarangTextField = new javax.swing.JTextField();
        hargaJualTextField = new javax.swing.JTextField();
        hargaBeliTextField = new javax.swing.JTextField();
        jumlahTextField = new javax.swing.JTextField();
        editButton = new javax.swing.JButton();
        simpanButton = new javax.swing.JButton();
        hapusButton = new javax.swing.JButton();
        keluarButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelbarang = new javax.swing.JTable();
        kodeBarangLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();

        jPanel1.setBackground(new java.awt.Color(153, 153, 255));

        kodeBarangLabel.setFont(new java.awt.Font("Showcard Gothic", 0, 24));
        kodeBarangLabel.setText("Data Barang ");

        namaBarangLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        namaBarangLabel.setText("Nama Barang");

        hargaBeliLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        hargaBeliLabel.setText("Harga Jual");

        hargaJualLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        hargaJualLabel.setText("Harga Beli");

        jumlahLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        jumlahLabel.setText("Jumlah");

        kodeBarangTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                kodeBarangTextFieldKeyReleased(evt);
            }
        });

        editButton.setText("Edit");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        simpanButton.setText("Simpan");
        simpanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simpanButtonActionPerformed(evt);
            }
        });

        hapusButton.setText("Hapus");
        hapusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hapusButtonActionPerformed(evt);
            }
        });

        keluarButton.setText("Keluar");
        keluarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keluarButtonActionPerformed(evt);
            }
        });

        tabelbarang.setBackground(new java.awt.Color(153, 153, 255));
        tabelbarang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Kode Barang", "Nama Barang", "Harga Beli", "Harga Jual", "Jumlah"
            }
        ));
        jScrollPane1.setViewportView(tabelbarang);

        kodeBarangLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
        kodeBarangLabel1.setText("Kode Barang ");

        jLabel1.setText("Berisi detail data barang, berfungsi untuk menyimpan data barang baru dan melihat stok barang");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(kodeBarangLabel)
                .addContainerGap(438, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 482, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(98, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(kodeBarangLabel1)
                            .addComponent(namaBarangLabel)
                            .addComponent(hargaBeliLabel)
                            .addComponent(hargaJualLabel)
                            .addComponent(jumlahLabel))
                        .addGap(30, 30, 30)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jumlahTextField)
                            .addComponent(hargaBeliTextField)
                            .addComponent(kodeBarangTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                            .addComponent(namaBarangTextField)
                            .addComponent(hargaJualTextField, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(180, 180, 180)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(hapusButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
                                .addComponent(keluarButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
                                .addComponent(editButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE))
                            .addComponent(simpanButton))))
                .addGap(68, 68, 68))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(kodeBarangLabel)
                .addGap(4, 4, 4)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kodeBarangLabel1)
                    .addComponent(kodeBarangTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(simpanButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(namaBarangLabel)
                    .addComponent(namaBarangTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(editButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hargaJualTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hargaBeliLabel)
                    .addComponent(hapusButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hargaBeliTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hargaJualLabel)
                    .addComponent(keluarButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jumlahTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jumlahLabel))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        jPanel1.setBounds(0, 0, 590, 360);
        dataBarangDesktopPane.add(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(dataBarangDesktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 588, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(dataBarangDesktopPane, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

private void simpanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simpanButtonActionPerformed
    try {
        String kode = kodeBarangTextField.getText();
        String nama = namaBarangTextField.getText();
        Double hargajual = Double.parseDouble(hargaJualTextField.getText());
        Double hargabeli = Double.parseDouble(hargaBeliTextField.getText());
        Double jumlah = Double.parseDouble(jumlahTextField.getText());

        Barang bar = new Barang();
        bar.setKodeBarang(kode);
        bar.setNamaBarang(nama);
        bar.setHargaBeli(hargajual);
        bar.setHargaJual(hargabeli);
        bar.setJumlah(jumlah);

        Barang bars = Barang.getBarang();
        bars.simpanBarangPrepared(bar);


        JOptionPane.showMessageDialog(this, "Data barang berhasil disimpan");
        hapusButton.doClick();
    } catch (SQLException ex) {
        Logger.getLogger(DataBarangForm.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(this, "\n Data barang gagal disimpan");
    }
}//GEN-LAST:event_simpanButtonActionPerformed

private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
    try {
        String kode = kodeBarangTextField.getText();
        String nama = namaBarangTextField.getText();
        Double hargajual = Double.parseDouble(hargaJualTextField.getText());
        Double hargabeli = Double.parseDouble(hargaBeliTextField.getText());
        Double jumlah = Double.parseDouble(jumlahTextField.getText());

        Barang bar = new Barang();
        bar.setKodeBarang(kode);
        bar.setNamaBarang(nama);
        bar.setHargaBeli(hargajual);
        bar.setHargaJual(hargabeli);
        bar.setJumlah(jumlah);

        Barang bars = Barang.getBarang();
        bars.cariBarang(bar);


        JOptionPane.showMessageDialog(this, "Data barang berhasil diubah");
        hapusButton.doClick();
    } catch (SQLException ex) {
        Logger.getLogger(DataBarangForm.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(this, "\n Data barang gagal diubah");
    }
    // TODO add your handling code here:
}//GEN-LAST:event_editButtonActionPerformed

private void hapusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hapusButtonActionPerformed
    kodeBarangTextField.setText(" ");
    namaBarangTextField.setText(null);
    hargaJualTextField.setText(null);
    hargaBeliTextField.setText(null);
    jumlahTextField.setText(null);// TODO add your handling code here:
}//GEN-LAST:event_hapusButtonActionPerformed

private void keluarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keluarButtonActionPerformed
    System.exit(0);    // TODO add your handling code here:
}//GEN-LAST:event_keluarButtonActionPerformed

private void kodeBarangTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_kodeBarangTextFieldKeyReleased
    try {
            // TODO add your handling code here:
            String kode = kodeBarangTextField.getText();
            Barang bar = new Barang();
            bar.setKodeBarang(kode);
            Barang.getBarang().cariBarang(bar);

            String nama = bar.getNamaBarang();
            Double hargajual = bar.getHargaJual();
            Double hargabeli = bar.getHargaBeli();
            Double jumlah = bar.getJumlah();

            namaBarangTextField.setText(nama);
            hargaJualTextField.setText(String.valueOf(hargajual));
            hargaBeliTextField.setText(String.valueOf(hargabeli));
            jumlahTextField.setText(String.valueOf(jumlah));
        } catch (SQLException ex) {
            Logger.getLogger(DataPemasokForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    // TODO add your handling code here:
}//GEN-LAST:event_kodeBarangTextFieldKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane dataBarangDesktopPane;
    private javax.swing.JButton editButton;
    private javax.swing.JButton hapusButton;
    private javax.swing.JLabel hargaBeliLabel;
    private javax.swing.JTextField hargaBeliTextField;
    private javax.swing.JLabel hargaJualLabel;
    private javax.swing.JTextField hargaJualTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel jumlahLabel;
    private javax.swing.JTextField jumlahTextField;
    private javax.swing.JButton keluarButton;
    private javax.swing.JLabel kodeBarangLabel;
    private javax.swing.JLabel kodeBarangLabel1;
    private javax.swing.JTextField kodeBarangTextField;
    private javax.swing.JLabel namaBarangLabel;
    private javax.swing.JTextField namaBarangTextField;
    private javax.swing.JButton simpanButton;
    private javax.swing.JTable tabelbarang;
    // End of variables declaration//GEN-END:variables
}
